import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {path: "/", redirect: "/login"},
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login/login.vue"),
  },
  {
    path: "/index",
    name: "index",

    component: () => import("../views/index/index.vue"),

    // 二级路由
    children: [
      {
        path: "/menu",
        name: "menu",
        meta: {
          title: "网站管理",
        },
        component: () => import("../views/menu/menu.vue"),
        },
        {
          path: "/role",
          name: "role",
          meta: {
            title: "用户管理",
          },
          component: () => import("../views/menu/role.vue"),
        },
        {
          path: "/user",
          name: "user",
          meta: {
            title: "角色管理",
          },
          component: () => import("../views/menu/user.vue"),
        },
        {
          path: "/menu",
          name: "menu",
          meta: {
            title: "菜单管理",
          },
          component: () => import("../views/menu/menu.vue"),
          
        }
        
          
       
      
      ]

  },
  {
    path: "/home",
    name: "home",
    component: () => import("../views/HomeView.vue"),
  },
  {
    path: "/about",
    name: "about",

    component: () => import("../views/AboutView.vue"),
  },
];

const router = new VueRouter({
  routes,
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // console.log(to, from);
  if(localStorage.getItem('token')){
    next()
  }else{
    if(to.path === '/login'){
      next()
    }else{
      next('/login')
    }
  }
});

export default router;
